package org.huoran.sakamichi.main.controller;

import org.huoran.sakamichi.main.rocketmq.producer.RocketMqProducer;
import org.huoran.sakamichi.main.util.ResultBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping({"/rocket"})
public class RocketmqController {

    @Resource
    private RocketMqProducer rocketMqProducer;

    @ResponseBody
    @RequestMapping(value = {"/sendMessage/{tag}/{content}"})
    public ResultBean sendMessage(@PathVariable String tag, @PathVariable String content) {
        try{
            content += " " + new Date().toString();
            Boolean success = this.rocketMqProducer.sendMessage(tag, content, UUID.randomUUID().toString());
            if(success){
                return ResultBean.success("send success");
            }
            else{
                return ResultBean.failed("send failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.failed("send failed");
        }
    }
}
